package com.ruoyi.system.service.honor.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.honor.studentreseach.StudentResearch;
import com.ruoyi.system.domain.honor.studentreseach.StudentResearchMember;
import com.ruoyi.system.mapper.honor.StudentResearchMapper;
import com.ruoyi.system.mapper.honor.StudentResearchMemberMapper;
import com.ruoyi.system.service.honor.IStudentResearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 指导学生科研成果科研项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class StudentResearchServiceImpl implements IStudentResearchService
{
    @Autowired
    private StudentResearchMapper studentResearchMapper;
    @Autowired
    private StudentResearchMemberMapper studentResearchMemberMapper;

    /**
     * 查询指导学生科研成果科研项目
     * 
     * @param id 指导学生科研成果科研项目主键
     * @return 指导学生科研成果科研项目
     */
    @Override
    public StudentResearch selectStudentResearchById(Long id)
    {
        StudentResearch studentResearch = studentResearchMapper.selectStudentResearchById(id);
        StudentResearchMember studentResearchMember = new StudentResearchMember();
        studentResearchMember.setClassId(id);
        studentResearch.setTeamMemberList(studentResearchMemberMapper.selectStudentResearchMemberList(studentResearchMember));
        return studentResearch;
    }

    /**
     * 查询指导学生科研成果科研项目列表
     * 
     * @param studentResearch 指导学生科研成果科研项目
     * @return 指导学生科研成果科研项目
     */
    @Override
    public List<StudentResearch> selectStudentResearchList(StudentResearch studentResearch)
    {
        if(studentResearch.getCurrentRole()!=null&&studentResearch.getCurrentRole().equals("teacher")){
            studentResearch.setTeacherId(String.valueOf(getUserId()));
        }
        //如果是部门，则显示老师是系里的所有
        if(studentResearch.getCurrentRole()!=null&&studentResearch.getCurrentRole().equals("department")){
            studentResearch.setDeptId(SecurityUtils.getDeptId());
        }
        return studentResearchMapper.selectStudentResearchList(studentResearch);
    }

    /**
     * 新增指导学生科研成果科研项目
     * 
     * @param studentResearch 指导学生科研成果科研项目
     * @return 结果
     */
    @Override
    public int insertStudentResearch(StudentResearch studentResearch)
    {
        int result = studentResearchMapper.insertStudentResearch(studentResearch);
        if (result > 0) {
            // 插入成功，获取刚插入的id
            Long studentResearchId = studentResearch.getId();
            // 插入关联表
            List<String> teamMembers = studentResearch.getTeamMembers();
            for (String teamMember : teamMembers) {
                StudentResearchMember studentResearchMember = new StudentResearchMember();
                studentResearchMember.setClassId(studentResearchId);
                studentResearchMember.setMemberName(teamMember);
                studentResearchMemberMapper.insertStudentResearchMember(studentResearchMember);
            }
        }
        return result;
    }

    /**
     * 修改指导学生科研成果科研项目
     * 
     * @param studentResearch 指导学生科研成果科研项目
     * @return 结果
     */
    @Override
    public int updateStudentResearch(StudentResearch studentResearch)
    {
        return studentResearchMapper.updateStudentResearch(studentResearch);
    }

    /**
     * 批量删除指导学生科研成果科研项目
     * 
     * @param ids 需要删除的指导学生科研成果科研项目主键
     * @return 结果
     */
    @Override
    public int deleteStudentResearchByIds(Long[] ids)
    {
        for(Long id : ids){
            StudentResearchMember studentResearchMember = new StudentResearchMember();
            studentResearchMember.setClassId(id);
            List<StudentResearchMember> studentResearchMembers = studentResearchMemberMapper.selectStudentResearchMemberList(studentResearchMember);
            for (StudentResearchMember studentResearchMember1 : studentResearchMembers) {
                studentResearchMemberMapper.deleteStudentResearchMemberById(studentResearchMember1.getId());
            }
        }
        return studentResearchMapper.deleteStudentResearchByIds(ids);
    }

    /**
     * 删除指导学生科研成果科研项目信息
     * 
     * @param id 指导学生科研成果科研项目主键
     * @return 结果
     */
    @Override
    public int deleteStudentResearchById(Long id)
    {
        return studentResearchMapper.deleteStudentResearchById(id);
    }
}
